from selenium import webdriver

import pytest
import time

from file_01 import *
from file_02 import login_about, register_about, card_about, buy_about, loan_about


class Test_one(login_about):

    @pytest.fixture(scope='function', params=['chrome', 'firefox'])
    def driver(self, request):
        if request.param == 'chrome':
            driver = webdriver.Chrome()
        elif request.param == 'firefox':
            driver = webdriver.Firefox()
        else:
            raise ValueError("ValueError")
        driver.get("http://localhost:90")
        driver.maximize_window()
        time.sleep(1)
        yield driver
        driver.quit()

    @pytest.fixture()
    def login_auto(self, driver):
        name = "lisi"
        password = "123456"
        self.login(driver, name, password)

    @pytest.fixture()
    def onedriver(self):
        onedriver = webdriver.Chrome()
        onedriver.get("http://localhost:90")
        onedriver.maximize_window()
        time.sleep(1)
        yield onedriver
        onedriver.quit()


class Test_login(Test_one, login_about):

    @pytest.mark.parametrize("name, pwd, expect", selects_from_excel(2, 3, 3))
    # @pytest.mark.parametrize("name, pwd, expect", select_from_yaml())
    def test_login(self, driver, name, pwd, expect):
        self.login(driver, name, pwd)
        res = self.getres_login(driver)
        self.review_login(res, expect)

    @pytest.mark.parametrize("name, pwd, expect", select_from_excel(4, 3))
    def test_login1(self, driver, name, pwd, expect):
        self.login(driver, name, pwd)
        res = self.getres_login1(driver)
        self.review_login(res, expect)


class Test_Register(register_about, Test_one):

    @pytest.mark.parametrize("name, pwd, repwd, expect", select_from_excel(6, 4))
    def test_register(self, onedriver, name, pwd, repwd, expect):
        self.open_register(onedriver)
        self.send_register(onedriver, name, pwd, repwd)
        res = self.getres_register(onedriver)
        self.review_register(res, expect)

    @pytest.mark.parametrize("name, pwd, repwd, expect", select_from_excel(7, 4))
    def test_register1(self, onedriver, name, pwd, repwd, expect):
        self.open_register(onedriver)
        self.send_register(onedriver, name, pwd, repwd)
        res = self.getres_register1(onedriver)
        self.review_register(res, expect)


class Test_card(Test_one, card_about):

    @pytest.mark.parametrize("bank, leixing, num", select_from_excel(9, 3))
    def test_card(self, driver, login_auto, bank, leixing, num):
        self.find_cardadd(driver)
        self.cardadd(driver, bank, leixing, num)
        res = self.getres_card(driver)
        self.review_card(res, bank, leixing, num)

    @pytest.mark.parametrize("bank, lei, num, expect", select_from_excel(10, 4))
    def test_card1(self, driver, login_auto, bank, lei, num, expect):
        self.find_cardadd(driver)
        self.cardadd(driver, bank, lei, num)
        res = self.getres_card1(driver)
        self.review_card1(res, expect)


class Test_buy(Test_one, buy_about):

    @pytest.mark.parametrize("name, pwd, num, pay, expect", select_from_excel(12, 5))
    def test_buy(self, driver, login_auto, name, pwd, num, pay, expect):
        self.find_buy(driver)
        self.buy_5(driver, num, pay)
        res = self.getres_buy(driver)
        self.review_buy(res, expect)

    @pytest.mark.parametrize("name, pwd, num, pay, expect", select_from_excel(13, 5))
    def test_buy1(self, driver, login_auto, name, pwd, num, pay, expect):
        self.find_buy(driver)
        self.buy_5(driver, num, pay)
        res = self.getres_buy(driver)
        self.review_buy(res, expect)


class Test_loan(Test_one, loan_about):

    @pytest.mark.parametrize("name, pwd, money, shijian, expect", select_from_excel(15, 5))
    def test_loan(self, driver, login_auto, name, pwd, money, shijian, expect):
        self.find_loan(driver)
        self.loan(driver, money, shijian)
        res = self.getres_loan(driver)
        self.review_loan(res, money, shijian)

    @pytest.mark.parametrize("name, pwd, money, shijian, expect", select_from_excel(16, 5))
    def test_loan1(self, driver, name, pwd, money, shijian, expect):
        self.login(driver, name, pwd)
        self.find_loan(driver)
        self.loan(driver, money, shijian)
        res = self.getres_loan1(driver)
        print(res)
        self.review_loan1(res, expect)
